/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.find.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.find.entity.GuideInfo;
import com.thinkgem.jeesite.modules.find.service.GuideInfoService;

/**
 * 考试指南Controller
 * @author 罗洪州
 * @version 2018-02-08
 */
@Controller
@RequestMapping(value = "${adminPath}/find/guideInfo")
public class GuideInfoController extends BaseController {

	@Autowired
	private GuideInfoService guideInfoService;
	
	@ModelAttribute
	public GuideInfo get(@RequestParam(required=false) String id) {
		GuideInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = guideInfoService.get(id);
		}
		if (entity == null){
			entity = new GuideInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("find:guideInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(GuideInfo guideInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<GuideInfo> page = guideInfoService.findPage(new Page<GuideInfo>(request, response), guideInfo); 
		model.addAttribute("page", page);
		return "modules/find/guideInfoList";
	}

	@RequiresPermissions("find:guideInfo:view")
	@RequestMapping(value = "form")
	public String form(GuideInfo guideInfo, Model model) {
		model.addAttribute("guideInfo", guideInfo);
		return "modules/find/guideInfoForm";
	}

	@RequiresPermissions("find:guideInfo:edit")
	@RequestMapping(value = "save")
	public String save(GuideInfo guideInfo, Model model, RedirectAttributes redirectAttributes) {
        guideInfo.setContent(StringEscapeUtils.unescapeHtml4(guideInfo.getContent().trim()));

        if(!(StringUtils.isEmpty(guideInfo.getImg()) || guideInfo.getImg().contains("http://"))) {
            guideInfo.setImg( Global.CURRENT_SERVER+guideInfo.getImg() );
        }
        if (!beanValidator(model, guideInfo)){
			return form(guideInfo, model);
		}
		guideInfoService.save(guideInfo);
		addMessage(redirectAttributes, "保存考试指南成功");
		return "redirect:"+Global.getAdminPath()+"/find/guideInfo/?repage";
	}
	
	@RequiresPermissions("find:guideInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(GuideInfo guideInfo, RedirectAttributes redirectAttributes) {
		guideInfoService.delete(guideInfo);
		addMessage(redirectAttributes, "删除考试指南成功");
		return "redirect:"+Global.getAdminPath()+"/find/guideInfo/?repage";
	}

}